<?php



namespace app\api\controller\pifa;



use app\common\controller\Api;

use think\Config;

use think\Db;

use think\Validate;

use think\Session;

use fast\WechatMini;

use addons\third\model\Third;

use think\Log;

use think\Request;

use fast\Http;

use think\Exception;

use addons\third\library\Service;
use fast\Huipaynew;


/**

 * 首页接口

 */

class Kehu extends Api

{

    protected $noNeedLogin = ['login','getconfig','getMendian1','token','laobaninit','taocanlist'];

    protected $noNeedRight = ['*'];





    public function _initialize()

    {

        $this->token = $this->request->post('token');



        parent::_initialize();



    } 



    //添加客户

    public function addkehu(){

        $post = file_get_contents( "php://input");

        $post = json_decode($post,true);

        

        $rule = [

                'consignee'  => 'require',

                'phone'  => 'require|regex:/^1\d{10}$/',

                // 'sex'     => 'require',

                'address'  => 'require',

        ];

        $msg = [         

                'consignee.require' => '请输入客户名称',

                'phone.require' => '请输入电话',

                'phone.regex' => '请输入正确的手机号',

                // 'sex.require' => '请选择性别',

                'address.require' => '请输入地址',

                

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }



        if (!empty($post['fileList1'])) {

            $params['image'] =  str_ireplace($this->request->root(true), '', $post['fileList1'][0]['url']);

        }



        $map_kehu['phone'] = $post['data']['phone'];


        $kehu = Db::table('fa_pifa_kehu')->where($map_kehu)->find();    


        if (!empty($kehu)) {
            $this->error("客户已存在");
        }
    

        $map_user['mobile'] = $post['data']['phone'];


        $kehu_user = Db::table('fa_user')->where($map_user)->find();

        
        if (!empty($kehu_user)) {
            $params['user_id'] = $kehu_user['id'];
            $params['status'] = '1';
        }
        $params['xiaoshou_id'] = $this->auth->id;
        $params['consignee'] = $post['data']['consignee'];
        $params['md_name'] = $post['data']['md_name'];
        $params['phone'] = $post['data']['phone'];


        $params['address'] = $post['data']['address'];

        $params['lat'] = $post['data']['lat'];

        $params['lng'] = $post['data']['lng'];
        $params['xiangxi'] = $post['data']['xiangxi'];
        $params['xing'] = $post['data']['xing'];


          

        $params['createtime'] = time();

        $params['updatetime'] = time();

        try {

            Db::table('fa_pifa_kehu')->insert($params);
            $this->success("修改成功");

        } catch (Exception $e) {
            $this->error($e->getMessage());

        }





           

    }



    //添加客户

    public function editkehu(){
        $post = file_get_contents( "php://input");
        $post = json_decode($post,true);
        $rule = [

                'consignee'  => 'require',
                'phone'  => 'require|regex:/^1\d{10}$/',
                'address'  => 'require',

        ];

        $msg = [         

                'consignee.require' => '请输入客户名称',
                'phone.require' => '请输入电话',
                'phone.regex' => '请输入正确的手机号',
                'address.require' => '请输入地址',

                

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }



        if (!empty($post['fileList1'])) {

            $params['image'] =  str_ireplace($this->request->root(true), '', $post['fileList1'][0]['url']);

        }

        $map_user['mobile'] = $post['data']['phone'];
        $kehu_user = Db::table('fa_user')->where($map_user)->find();

        
        if (!empty($kehu_user)) {
            $params['user_id'] = $kehu_user['id'];
            $params['status'] = '1';
        }
        $params['xiaoshou_id'] = $this->auth->id;
        $params['consignee'] = $post['data']['consignee'];
        $params['md_name'] = $post['data']['md_name'];
        $params['phone'] = $post['data']['phone'];


        $params['address'] = $post['data']['address'];

        $params['lat'] = $post['data']['lat'];

        $params['lng'] = $post['data']['lng'];
        $params['xing'] = $post['data']['xing'];
        $params['xiangxi'] = $post['data']['xiangxi'];



          


            $params['updatetime'] = time();



            try {
                $map['id'] = $post['data']['id'];
                Db::table('fa_pifa_kehu')->where($map)->update($params);

                $this->success("提交成功");

            } catch (Exception $e) {



                $this->error($e->getMessage());

            }





           

    }

        //添加客户

    public function genjin(){
        $post = file_get_contents( "php://input");
        $post = json_decode($post,true);
        $rule = [

                'zhuangtai'  => 'require',
                'type_name'  => 'require',
                'emo'  => 'require',

        ];

        $msg = [         

                'zhuangtai.require' => '请选择客户状态',
                'type_name.require' => '请选择跟进类型',
                'emo.require' => '请输入跟进内容',

                

        ];

        $data = $post['data'];

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }



        if (empty($post['fileList1'])) {
            $this->error('请上传附件');
            $params['image'] =  str_ireplace($this->request->root(true), '', $post['fileList1'][0]['url']);

        }

        foreach ($post['fileList1'] as $key => $value) {
           $params['images'][$key] = str_ireplace($this->request->root(true), '', $value['url']);
        }


        $params['user_id'] = $this->auth->id;
        $params['kehu_id'] = $post['data']['kehu_id'];
        $params['zhuangtai'] = $post['data']['zhuangtai'];
        $params['type_name'] = $post['data']['type_name'];
        $params['next_time'] = $post['data']['next_time'];
        $params['emo'] = $post['data']['emo'];
        $params['createtime'] = time();






        try {
            Db::table('fa_pifa_genjin')->insert($params);

            $map['id'] = $post['data']['kehu_id'];
            $updata['zhuangtai'] = $post['data']['zhuangtai'];
            if (!empty($post['data']['next_time'])) {
                $updata['next_time'] = $post['data']['next_time'];
            }
            Db::table('fa_pifa_kehu')->where($map)->update($updata);
            $this->success("提交成功");
        } catch (Exception $e) {
            $this->error($e->getMessage());

        }





           

    }
    




    public function genjinlist()

    {
        $post = $this->request->post();
        $map['kehu_id'] = $post['id'];

        $Genjin = new \app\admin\model\pifa\Genjin;

        $list = $Genjin
                    ->with(['user','kehu'])
                    ->where($map)
                    ->order('createtime desc')
                    ->paginate(12);

        foreach ($list as $row) {
                
            $row->getRelation('user')->visible(['username','mobile','avatar','nickname']);
            $row->getRelation('kehu')->visible(['consignee','phone']);
        }
        $list = $list->toArray();

        foreach ($list['data'] as $key => $value) {
            $images = [];

            if (!empty($value['images'])) {
                $images = explode(',',$value['images']);

                // var_dump($value['images']);
                foreach ($images as $key1 => $value1) {
                    $images[$key1] = cdnurl($value1);
                }
            }
            $list['data'][$key]['images'] = $images;
        }
        $this->success("成功", $list);

    }

      //我的客户

     public function my_kehu()

    {

        $map['xiaoshou_id'] = $this->auth->id;

       $Kehu = new \app\admin\model\pifa\Kehu;

        $post = $this->request->request();



        if (!empty($post['keyword'])) {
           $map['fa_pifa_kehu.consignee'] = ['like','%'.$post['keyword'].'%'];
        }


        if ($post['orderstate'] != '-1') {

            switch ($post['orderstate']) {
                case 1:
                    $map_1['inv_id'] = $this->auth->id;
                    $user_ids = Db::table('fa_user')->where($map_1)->column('id');
                    if (!empty($user_ids)) {
                        $map['xiaoshou_id'] = ['in',$user_ids];
                    }else{
                         $map['xiaoshou_id'] = -1;
                    }
                break;

                case 2:
                    $map_1['paystate'] = '1';
                    $user_ids = Db::table('fa_pifa_order')->where($map_1)->column('user_id');
                    if (!empty($user_ids)) {
                        $map['user_id'] = ['in',$user_ids];
                    }
                break;
                
   
            }
          

        }




        $list = $Kehu
                    ->with(['user'])
                    ->where($map)
                    ->order('createtime desc')
                    ->paginate(12);


        foreach ($list as $row) {
            $row->getRelation('user')->visible(['nickname','mobile','avatar']);

        }

        $list = $list->toArray();



        foreach ($list['data'] as $key => $value) {

           $list['data'][$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);

        }





      $this->success("成功", $list);

    }

    

}

